【例子介绍
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Linq;using System.Windows.Forms;using DevExpress.XtraCharts;using DevExpress.XtraEditors;namespace ChartControlTest{ public partial class CrimeForm : DevExpress.XtraEditors.XtraForm { GetNewData getNewData = new GetNewData(); private int seletedIndex = 0; public CrimeForm() { InitializeComponent(); BindDataByPrivince(); } private void BindDataByPrivince() { var dt = GetCrimEventByProvince(); chartControl1.Series.Clear(); var series1 = CreateNewSeries("各省近年来暴力事件汇总", ViewType.Bar, dt, "StrikeWhere", "EventCount"); chartControl1.Series.Add(series1); chartControl2.Series.Clear(); var series2 = CreateNewSeries("各省近年来暴力事件汇总", ViewType.Pie3D, dt, "StrikeWhere", "EventCount"); chartControl2.Series.Add(series2); chartControl3.Series.Clear(); var series3 = CreateNewSeries("各省近年来暴力事件汇总", ViewType.Line, dt, "StrikeWhere", "EventCount"); chartControl3.Series.Add(series3); //// 柱状图里的第一个柱 //var series1 = new Series("各省近年来暴力事件汇总", ViewType.Bar) // { // DataSource = GetCrimEventByProvince(), // ArgumentScaleType = ScaleType.Qualitative, // ArgumentDataMember = "StrikeWhere", // ValueScaleType = ScaleType.Numerical, // ShowInLegend = true, // Label = { Visible = true } // }; //series1.ValueDataMembers.AddRange(new string[] { "EventCount" }); //chartControl1.Series.Add(series1); //chartControl2.Series.Clear(); //var series2 = new Series("各省近年来暴力事件汇总", ViewType.Pie3D) // { // DataSource = GetCrimEventByProvince(), // ArgumentScaleType = ScaleType.Qualitative, // ArgumentDataMember = "StrikeWhere", // ValueScaleType = ScaleType.Numerical, // ShowInLegend = true, // Label = { Visible = true }, // LegendPointOptions = { PointView = PointView.ArgumentAndValues } // }; //series2.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; //series2.ValueDataMembers.AddRange(new string[] { "EventCount" }); //chartControl2.Series.Add(series2); //chartControl3.Series.Clear(); //var series3 = new Series("各省近年来暴力事件汇总", ViewType.Line) //{ // DataSource = GetCrimEventByProvince(), // ArgumentScaleType = ScaleType.Qualitative, // ArgumentDataMember = "StrikeWhere", // ValueScaleType = ScaleType.Numerical, // ShowInLegend = true, // Label = { Visible = true } //}; //series3.ValueDataMembers.AddRange(new string[] { "EventCount" }); //chartControl3.Series.Add(series3); } private Series CreateNewSeries(string title, ViewType type, DataTable dt, string argumentDataMemberName, string valueDataMemberName) { var series = new Series(title, type) { DataSource = dt, ArgumentScaleType = ScaleType.Qualitative, ArgumentDataMember = argumentDataMemberName, ValueScaleType = ScaleType.Numerical, ShowInLegend = true, Label = { Visible = true } }; if (type == ViewType.Pie3D) { series.LegendPointOptions.PointView = PointView.ArgumentAndValues; series.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; } series.ValueDataMembers.AddRange(new string[] { valueDataMemberName }); return series; } private void BindDataByMonth() { var dt = GetCrimEventByMouth(); chartControl1.Series.Clear(); var series1 = CreateNewSeries("近年来各月暴力事件汇总", ViewType.Bar, dt, "Month", "EventCount"); chartControl1.Series.Add(series1); chartControl2.Series.Clear(); var series2 = CreateNewSeries("近年来各月暴力事件汇总", ViewType.Pie3D, dt, "Month", "EventCount"); chartControl2.Series.Add(series2); chartControl3.Series.Clear(); var series3 = CreateNewSeries("近年来各月暴力事件汇总", ViewType.Line, dt, "Month", "EventCount"); chartControl3.Series.Add(series3); } //按照所在地区分组统计 private DataTable GetCrimEventByProvince() { var ds = getNewData.GetDataSourceOfCrime(); var dt = ds.Tables[0]; var query = from t in dt.AsEnumerable() group t by new { t1 = t.Field<string>("StrikeWhere") } into m select new { StrikeWhere = m.Key.t1, Count = m.Count() //StaffNum = m.Sum(n => n.Field<int>("StaffNum")) }; var newDt = getNewData.CreateShowDataTableByPrivince(); if (query.ToList().Count > 0) { for (int i = 0; i < query.Count(); i ) { var row = newDt.NewRow(); row["StrikeWhere"] = query.ToList()[i].StrikeWhere; row["EventCount"] = query.ToList()[i].Count; newDt.Rows.Add(row); } } return newDt; } //按照月份分组 private DataTable GetCrimEventByMouth() { var ds = getNewData.GetDataSourceOfCrime(); var dt = ds.Tables[0]; var dtNew = dt.Copy(); var column = new DataColumn { DataType = System.Type.GetType("System.Int32"), ColumnName = "Month", AutoIncrement = false, Caption = "所属月份", ReadOnly = false, Unique = false }; dtNew.Columns.Add(column); for (int i = 0; i < dtNew.Rows.Count; i ) { dtNew.Rows[i]["Month"] = DateTime.Parse(dtNew.Rows[i]["StrikeWhen"].ToString()).Month; } var query = from t in dtNew.AsEnumerable() group t by new { t1 = t.Field<Int32>("Month") } into m select new { Month = m.Key.t1, Count = m.Count() }; var newDt = getNewData.CreateShowDataTableByMonth(); if (query.ToList().Count > 0) { query = query.ToList().OrderBy(p => p.Month); for (int i = 0; i < query.Count(); i ) { var row = newDt.NewRow(); row["Month"] = string.Format("{0}月", query.ToList()[i].Month); row["EventCount"] = query.ToList()[i].Count; newDt.Rows.Add(row); } } return newDt; } private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) { var nowSeltIndex = comboBoxEdit1.SelectedIndex; if(nowSeltIndex==seletedIndex) return; seletedIndex = nowSeltIndex; if (seletedIndex == 0) BindDataByPrivince(); else BindDataByMonth(); } }}
评论